
package server;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class provides the functionality to connect the webserver 
 * to the actual java files, which gets the user names and other 
 * related info and creates users
 * @author Valerie
 */
public class UserCreateServlet extends HttpServlet {
    private Admin adm = new Admin();
    	
    /**
     * Creates a new user. Takes in parameters from the request. If they are all valid and the user does not yet exist,
     * creates a new user. If not, returns the response with an error message.
     * 
     * 
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
        	           try{
                              
        String name = request.getParameter("fname");
        Cookie ckname = new Cookie("name", name);
        response.addCookie(ckname);
        String email = request.getParameter("email");
        Cookie ckemail = new Cookie("email", email);
        response.addCookie(ckemail);
        String password = request.getParameter("pword");
        String pwtwo = request.getParameter("pwordconfirm");
        String zonenum = request.getParameter("zonenum");
        String address = request.getParameter("address");
        Cookie ckaddress = new Cookie("address", address);
        response.addCookie(ckaddress);
        String interests = request.getParameter("interests");
        Cookie ckinterests = new Cookie("interests", interests);
        response.addCookie(ckinterests);
        if(name==null || name.trim().isEmpty() || !name.matches("\\p{Alnum}+")){
            response.sendRedirect("register.jsp?msg=name");
        } else if (email==null || email.trim().isEmpty() || !email.matches("([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})")){
            response.sendRedirect("register.jsp?msg=email");
        } else if (password==null || password.trim().isEmpty() || !password.matches("\\p{Alnum}+") ){
            response.sendRedirect("register.jsp?msg=password");
        } else if(zonenum==null || zonenum.trim().isEmpty() || !zonenum.matches("0*([1][0-2]|[0-9])")){
            response.sendRedirect("register.jsp?msg=zonenum");
        } else if(address==null || address.trim().isEmpty()){
            response.sendRedirect("register.jsp?msg=address");
        }   else{
            if (interests==null || interests.trim().isEmpty()){
            interests= "";
            }
            if(!password.equals(pwtwo)){
                response.sendRedirect("register.jsp?msg=pword");
            }
            else if(adm.register(name, email, password, Integer.parseInt(zonenum), address, interests)){
                ckname.setMaxAge(0);
                response.addCookie(ckname);
                ckemail.setMaxAge(0);
                response.addCookie(ckemail);
                ckaddress.setMaxAge(0);
                response.addCookie(ckaddress);
                ckinterests.setMaxAge(0);
                response.addCookie(ckinterests);
                response.sendRedirect("index.jsp");
            }else{
                response.sendRedirect("register.jsp?msg=bad");
            }
        }
        } finally{
                
            }
            

	}

	/**
         * Does nothing
         * 
	 * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
